package com.topnav.mc.zzd;

import com.alibaba.fastjson2.JSONObject;
import com.js.lib.result.Result;
import com.js.lib.utils.DateUtils;
import com.topnav.mc.admin.security.PermissionGrade;
import com.topnav.mc.exception.ExceptionMsg;
import com.topnav.mc.uitls.ErrorResultUtil;
import com.topnav.mc.uitls.ExceptionUtil;
import com.topnav.mc.uitls.PermissionGradeUtil;
import com.topnav.mc.uitls.RedisUtil;
import com.topnav.mc.zzd.account.ZzdAccountUntil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.async.DeferredResult;
import javax.annotation.Resource;
import java.net.URLEncoder;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 这些是测试使用
 * @Author: kenny
 * @Date: 2024/1/8 17:40
 */
@Deprecated
@RestController
@CrossOrigin
@RequestMapping({"/test/zzd","/zzd"})
@Slf4j
public class ZzdContoller {
    @Resource
    public ZzdApiUrl zzdApiUrl;
    @Autowired
    ZzdAccountUntil zzdAccountUntil;

    /**
     * 用户查询
     * @return
     */
    @GetMapping({"/query/users"})
    @ResponseBody
    public DeferredResult<Result> queryUsers(){
        String start = DateUtils.format(new Date(),"HH:mm:ss.SSS");
        DeferredResult<Result> result = new DeferredResult<Result>(5000L);
        result.onTimeout(()->{
            result.setResult(Result.error(String.format("time out: %s==>%s",start,DateUtils.format(new Date(),"HH:mm:ss.SSS"))));
        });
        Map<String,Object> map = new HashMap<>();
        map.put("limit",50);
        map.put("page",0);
        map.put("group_id",883510);
        String url=zzdApiUrl.queryUser(map);
        ZzdHttpUtil.request(url, null,zzdAccountUntil.getZzdAccount(),new ZzdHttpUtil.ApiCallBack() {
            @Override
            public void onResult(String resultJson) {//成功
                System.out.println(resultJson);
                JSONObject json = JSONObject.parseObject(resultJson);
                result.setResult(Result.ok(json.getInteger("code"),json.getString("msg"),json.getJSONObject("data")));
            }
        },  new ZzdHttpUtil.ApiCallBack() {

            @Override
            public void onResult(String resultJson) {//失败
                System.out.println(resultJson);
                JSONObject json = JSONObject.parseObject(resultJson);
                result.setResult(Result.error(Result.ok(json.getInteger("code"),json.getString("msg"),json.getJSONObject("data"))));
            }
        });
        return result;
    }

    @GetMapping({"/new/user"})
    @ResponseBody
    public DeferredResult<Result> newUser(){
        String start = DateUtils.format(new Date(),"HH:mm:ss.SSS");
        DeferredResult<Result> result = new DeferredResult<Result>(5000L);
        result.onTimeout(()->{
            result.setResult(Result.error(String.format("time out: %s==>%s",start,DateUtils.format(new Date(),"HH:mm:ss.SSS"))));
        });
        String random = (String)RedisUtil.get(ZzdConstant.ZZD_RANDOM_KEY);
        if (random == null){

            result.setResult(Result.error("第三方系统未登录"));
        }

        Map<String,Object> map = new HashMap<>();
        map.put("user_account","44010200492000000020");
        map.put("pwd",ZzdHttpUtil.sha1("111111"));
        map.put("user_name","test-测试用户");
        map.put("proxy_account",zzdAccountUntil.getZzdAccount().getAccount());
        map.put("proxy_pwd",ZzdHttpUtil.hmacSha1(random, ZzdHttpUtil.sha1(zzdAccountUntil.getZzdAccount().getPassword())));
        map.put("bill_id",1);
        map.put("bill_type",1);
        map.put("clientType",3);
        map.put("group_ids","883510");
        map.put("desc","test-测试用户");
        String url=zzdApiUrl.newUser(map);
        ZzdHttpUtil.request(url, null,zzdAccountUntil.getZzdAccount(),new ZzdHttpUtil.ApiCallBack() {
            @Override
            public void onResult(String resultJson) {//成功
                System.out.println(resultJson);
                result.setResult(genResult(resultJson));
            }
        },  new ZzdHttpUtil.ApiCallBack() {
            @Override
            public void onResult(String resultJson) {//失败
                System.out.println(resultJson);
                result.setResult(genResult(resultJson));
            }
        });
        return result;
    }
    /**
     * 群组用户设置
     * @return
     */
    @GetMapping({"/new/groupusers"})
    @ResponseBody
    public DeferredResult<Result> newGroupUsers(){
        String start = DateUtils.format(new Date(),"HH:mm:ss.SSS");
        DeferredResult<Result> result = new DeferredResult<Result>(5000L);
        result.onTimeout(()->{
            result.setResult(Result.error(String.format("time out: %s==>%s",start,DateUtils.format(new Date(),"HH:mm:ss.SSS"))));
        });
        Map<String,Object> map = new HashMap<>();
        map.put("group_id",883510);
        map.put("user_ids","2984574980,2984574979");
        String url=zzdApiUrl.newGroupUser(map);
        ZzdHttpUtil.request(url, null,zzdAccountUntil.getZzdAccount(),new ZzdHttpUtil.ApiCallBack() {
            @Override
            public void onResult(String resultJson) {//成功
                System.out.println(resultJson);
                result.setResult(genResult(resultJson));
            }
        },  new ZzdHttpUtil.ApiCallBack() {

            @Override
            public void onResult(String resultJson) {//失败
                System.out.println(resultJson);
                result.setResult(genResult(resultJson));
            }
        });
        return result;
    }

    @GetMapping({"/delete/groupusers"})
    @ResponseBody
    public DeferredResult<Result> deleteGroupUser(){
        String start = DateUtils.format(new Date(),"HH:mm:ss.SSS");
        DeferredResult<Result> result = new DeferredResult<Result>(5000L);
        result.onTimeout(()->{
            result.setResult(Result.error(String.format("time out: %s==>%s",start,DateUtils.format(new Date(),"HH:mm:ss.SSS"))));
        });
        Map<String,Object> map = new HashMap<>();
        map.put("group_id",883510);
        map.put("user_ids","2984574980");
        String url=zzdApiUrl.deleteGroupUser(map);
        ZzdHttpUtil.request(url, null,zzdAccountUntil.getZzdAccount(),new ZzdHttpUtil.ApiCallBack() {
            @Override
            public void onResult(String resultJson) {//成功
                System.out.println(resultJson);
                result.setResult(genResult(resultJson));
            }
        },  new ZzdHttpUtil.ApiCallBack() {

            @Override
            public void onResult(String resultJson) {//失败
                System.out.println(resultJson);
                result.setResult(genResult(resultJson));
            }
        });
        return result;
    }


    /**
     * 用户群组
     * @return
     */
    @GetMapping({"/query/group"})
    @ResponseBody
    public DeferredResult<Result> queryGroup(){
        String start = DateUtils.format(new Date(),"HH:mm:ss.SSS");
        DeferredResult<Result> result = new DeferredResult<Result>(5000L);
        result.onTimeout(()->{
            result.setResult(Result.error(String.format("time out: %s==>%s",start,DateUtils.format(new Date(),"HH:mm:ss.SSS"))));
        });
        Map<String,Object> map = new HashMap<>();
        map.put("limit",50);
        map.put("page",0);
        String url=zzdApiUrl.queryGroup(map);
        ZzdHttpUtil.request(url, null,zzdAccountUntil.getZzdAccount(),new ZzdHttpUtil.ApiCallBack() {
            @Override
            public void onResult(String resultJson) {//成功
                System.out.println(resultJson);
                result.setResult(genResult(resultJson));
            }
        },  new ZzdHttpUtil.ApiCallBack() {

            @Override
            public void onResult(String resultJson) {//失败
                System.out.println(resultJson);
                result.setResult(genResult(resultJson));
            }
        });
        return result;
    }

    /**
     * 加群组
     * @return
     */
    @PostMapping({"/new/group"})
    @ResponseBody
    public DeferredResult<Result> newGroup(@RequestBody Map<String,Object> param){
        String start = DateUtils.format(new Date(),"HH:mm:ss.SSS");
        DeferredResult<Result> result = new DeferredResult<Result>(5000L);
        result.onTimeout(()->{
            result.setResult(ErrorResultUtil.result(
                    ExceptionMsg.ERROR_LINK_D3PART_EXPIRE,
                    String.format("time out: %s==>%s",start,DateUtils.format(new Date(),"HH:mm:ss.SSS"))));
        });
        //Map<String,Object> map = new HashMap<>();
        if (param.get("groupName") != null){
            param.put("contactName", URLEncoder.encode(param.get("groupName").toString()));
        }
        if (param.get("contactName") != null){
            param.put("contactName", URLEncoder.encode(param.get("contactName").toString()));
        }
        if (param.get("contactPhone") != null){
            param.put("contactPhone", URLEncoder.encode(param.get("contactPhone").toString()));
        }
        //map.put("group_name", URLEncoder.encode("测试群组-05"));
        //map.put("contact_name","1");

        String url=zzdApiUrl.newGroup(param);
        ZzdHttpUtil.request(url, null,zzdAccountUntil.getZzdAccount(),new ZzdHttpUtil.ApiCallBack() {
            @Override
            public void onResult(String resultJson) {//成功
                System.out.println(resultJson);
                result.setResult(genResult(resultJson));
            }
        },  new ZzdHttpUtil.ApiCallBack() {

            @Override
            public void onResult(String resultJson) {//失败
                System.out.println(resultJson);
                result.setResult(genResult(resultJson));
            }
        });
        return result;
    }

    /**
     * 删除群组
     * @return
     */
    @PostMapping({"/delete/group/{groupId}"})
    @ResponseBody
    public DeferredResult<Result> deleteGroup(@PathVariable Integer groupId){
        String start = DateUtils.format(new Date(),"HH:mm:ss.SSS");
        DeferredResult<Result> result = new DeferredResult<Result>(5000L);
        result.onTimeout(()->{
            result.setResult(Result.error(String.format("time out: %s==>%s",start,DateUtils.format(new Date(),"HH:mm:ss.SSS"))));
        });
        Map<String,Object> map = new HashMap<>();
        map.put("group_ids",groupId);
        String url=zzdApiUrl.deleteGroup(map);
        ZzdHttpUtil.request(url, null,zzdAccountUntil.getZzdAccount(),new ZzdHttpUtil.ApiCallBack() {
            @Override
            public void onResult(String resultJson) {//成功
                System.out.println(resultJson);
                result.setResult(Result.ok(JSONObject.parseObject(resultJson)));
            }
        },  new ZzdHttpUtil.ApiCallBack() {
            @Override
            public void onResult(String resultJson) {//失败
                System.out.println(resultJson);
                result.setResult(genResult(resultJson));
            }
        });
        return result;
    }

    /**
     * 用户查询
     * @return
     */
    @GetMapping({"/query/order"})
    @ResponseBody
    public DeferredResult<Result> queryOrder(){
        String start = DateUtils.format(new Date(),"HH:mm:ss.SSS");
        DeferredResult<Result> result = new DeferredResult<Result>(5000L);
        result.onTimeout(()->{
            result.setResult(Result.error(String.format("time out: %s==>%s",start,DateUtils.format(new Date(),"HH:mm:ss.SSS"))));
        });
        Map<String,Object> map = new HashMap<>();
        map.put("limit",50);
        map.put("page",0);
        String url=zzdApiUrl.queryOrder(map);
        ZzdHttpUtil.request(url, null,zzdAccountUntil.getZzdAccount(),new ZzdHttpUtil.ApiCallBack() {
            @Override
            public void onResult(String resultJson) {//成功
                System.out.println(resultJson);
                result.setResult(genResult(resultJson));
            }
        },  new ZzdHttpUtil.ApiCallBack() {

            @Override
            public void onResult(String resultJson) {//失败
                System.out.println(resultJson);
                result.setResult(genResult(resultJson));
            }
        });
        return result;
    }
    private Result genResult(String resutStr){
        JSONObject json = JSONObject.parseObject(resutStr);
        if (json.getInteger("code") != null){
            if (json.getInteger("code") ==0){
                return Result.ok(0,json.getString("msg"),json.getJSONObject("data"));
            }else{
                return Result.error(json.getInteger("code"),json.getString("msg"),json.getJSONObject("data"));
            }
        }else{
            return Result.error(-1,json.getString("msg"),json.getJSONObject("data"));
        }
    }

}
